package modes

import (
	"fmt"
	"time"
)

/*课程收藏记录*/
type Collen struct {
	Id        int64  `json:"coll_id" xorm:"id"`                 //  课程收藏id
	UId       int64  `json:"coll_uid" xorm:"uid"`               //  学员id
	SId       int64  `json:"coll_sid" xorm:"sid"`               //  课次id
	CId       int64  `json:"coll_cid" xorm:"cid"`               //  班型id(无用)
	Name      string `json:"coll_name" xorm:"name"`             //  课次名称
	LectureAt int64  `json:"coll_lecture_at" xorm:"lecture_at"` //  听课时长
	Url       string `json:"coll_url" xorm:"url"`               //  url
	At        int64  `json:"coll_at" xorm:"at"`                 //  创建时间
	KcId      int64  `json:"kc_id" xorm:"kc_id"`                // 课程id
	Type      int64  `json:"type" xorm:"type"`                  //  类型 1录播 2直播

}

func (this *Collen) TableName() string {
	return "collen"
}
func (this *Collen) Save() (int64, error) {
	this.At = time.Now().Unix()
	return Db(0).Insert(this)
}

func (this *Collen) Get() (bool, error) {
	return Db(0).Get(this)
}

/**
获取课程id列表
*/
//  SELECT  DISTINCT (sid-sid%1000)  FROM `collen`
func (this *Collen) GetCollSid2List() ([]int64, error) {
	sidList := make([]int64, 0)
	err := Db(0).Table(this.TableName()).Where("uid=?", this.UId).Select("distinct (kc_id-kc_id%1000) ").Find(&sidList)
	return sidList, err
}

/**
获取某课程下的课次的id
*/
func (this *Collen) GetCollBySidList(courseId int64) ([]Collen, error) {
	sidList := make([]Collen, 0)

	where := fmt.Sprintf(" uid=%d and kc_id=%d", this.UId, courseId)
	err := Db(0).Table(this.TableName()).Desc("at").Where(where).Find(&sidList)
	return sidList, err
}

func (this *Collen) GetCollBySidsList(courseId int64) ([]Collen, error) {
	sidList := make([]Collen, 0)

	where := fmt.Sprintf(" uid=%d and sid>%d and sid<%d ", this.UId, courseId, (courseId + 1000))
	err := Db(0).Table(this.TableName()).Where(where).Find(&sidList)
	return sidList, err
}

func (this *Collen) GetCollByCidList(cId int64) ([]Collen, error) {
	sidList := make([]Collen, 0)

	where := fmt.Sprintf(" uid=%d and kc_id>%d and kc_id<%d ", this.UId, cId, (cId + 100000))
	err := Db(0).Table(this.TableName()).Desc("at").Where(where).Find(&sidList)
	return sidList, err
}

//  科目id SELECT  DISTINCT (sid-sid%100000)  FROM `collen`
func (this *Collen) GetCollSidList() ([]int64, error) {
	sidList := make([]int64, 0)
	err := Db(0).Distinct("(sid-sid%100000) ").Find(&sidList)
	return sidList, err
}
func (this *Collen) delete(where string) (int64, error) {
	return Db(0).Where(where).Delete(this)
}
func (this *Collen) Del() (int64, error) {
	where := fmt.Sprintf("id =%d", this.Id)
	return this.delete(where)
}
func (this *Collen) DelByUidSid(sidlist []int64) (int64, error) {
	return Db(0).In("sid", sidlist).Delete(this)
}
func (this *Collen) Count(where string) (int64, error) {
	var val Collen
	return Db(0).Where(where).Count(&val)
}
func (this *Collen) update(where string, field string) (int64, error) {
	return Db(0).Where(where).Cols(field).Update(this)
}

func (this *Collen) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *Collen) where(fage, count, page int, where, field string) ([]Collen, error) {
	list := make([]Collen, 0)
	var err error
	if field == "" {
		field = "id"
	}
	if 0 == fage { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}
func (this *Collen) List(val *Pager) error {
	val.TName = this.TableName()
	w := fmt.Sprintf("uid = %d ", this.UId)
	val.Total, _ = this.Count(w)
	list, err := this.where(val.Rules, val.Count, val.Page-1, w, "")
	if err == nil {
		val.List = list
	}
	return err
}

func (this *Collen) ListByUid() ([]Collen, error) {
	if this.UId <= 0 {
		return nil, nil
	}
	w := fmt.Sprintf("uid = %d ", this.UId)
	list := make([]Collen, 0)
	err := Db(0).Where(w).Find(&list)
	return list, err
}

func (this *Collen) Lookups(val *Pager) error {
	list := make([]Collen, 0)
	val.TName = "collen"
	val.Page--
	var field, sort string
	switch val.Coll {
	case 0:
		field = fmt.Sprintf("%s.at", val.TName) // 时间排序
	default:
		field = fmt.Sprintf("%s.at", val.TName) // 状态
	}
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s asc", field)
	} else {
		sort = fmt.Sprintf("%s desc", field)
	}
	val.Total, _ = Db(0).Table(val.TName).Where(val.ToWhere()).Count(this)
	err := Db(0).Table(val.TName).
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err

}
